<?php

/*
 * 商家模型
 * @author wyb
 * 参考用 不参与实际生产
 */

namespace DataAPI\Model;

use DataAPI\Model\ErpApiModel;
use ExtendApi;
class WestpayApiModel
{

    protected $model;
    protected $erpApiObject;
    function __construct($api_id = 1)
    {
        set_time_limit(0);
        $this->model = M('supplier_products');
        $this->erpApiObject = new ErpApiModel();
    }

    public function getNewGoodsList()
    {
        for ($i = 1; $i <= 100000; $i++) {
            $result = \DataAPI\Extend\Xilian::init()->get_commodity($i);
            if ($result['data']) {
                foreach ($result['data'] as $key => $value) {
                    if (!$this->model->where(array('enren13' => $value['Code']))->find()) {
                        $this->model->add(array(
                            'enrn13' => $value['Code'],
                            'name' => $value['Name'],
                            'weight' => $value['Spec'],
                            'info_keywords'=>'',
                            'pom' => $value['Jjfs'],
                            'info_pc' => '',
                            'info_mobile' => '',
                            'market_price' => '',
                            'acting_price' => '',
                            'wholesale_price' =>'',
                            'lock_price' =>'',
                            'wholesale_s' => '',
                            'wholesale_start_num' => '',
                            'lock_start_num' => '',
                            'price' => '',
                            'stock_num' => '',
                            'warehouse_id' => '',
                        ));
                    }
                    unset($result['data'][$key]);
                }
            } else {
                break;
            }
        }
    }
    public function isShopExits(){
        $shop_info = \DataAPI\Extend\Xilian::init()->get_shop();
        foreach( $shop_info['data'] as $m => $n ) {
            //判断店铺是否存在
            if( !M('suplier')->where(array('name' =>  $n['Name'], 'code' => $n['Organ']))->find() ) {
                //不存在 添加
                $insert_id = M('suplier')->add(
                    array(
                        'name' => $n['Name'],
                        'code' => $n['Organ'],
                        'user'  => '西联',
                        'tel' => $n['Organ']
                    )
                );
                $this->erpApiObject->supplier_add( $insert_id ); //erp添加供应商
            }
        }
    }

    public function syncShop(){
        $shop_info = \DataAPI\Extend\Xilian::init()->get_shop();
        //同步店铺代码以及价格
        foreach( $shop_info['data'] as $m => $n ) {
            for( $i = 1; $i <= 1000000; $i++ ) {
                $goods_info = \DataAPI\Extend\Xilian::init()->get_commdity_shop( $n['Organ'] , $i );
                if( isset( $goods_info['data'] ) && $goods_info['data'] ) {
                    foreach( $goods_info['data'] as $k => $v ) {
                        $this->model->where(array('eran13' => $v['Code']))->save(array('price' => $v['Price'] , 'member_price' => $v['Memprice']));
                    }
                    unset($goods_info['data'][$k]);
                } else {
                    break;
                }
            }
        }
    }


    //写入商品库
    public function insertGoodas(){
        $data = $this->model->where(array( '_string' => 'stock > 0 AND is_sync = 0' ))->select();
        foreach( $data as $key => $value ) {
            $id = M('country')->where(array('Chinese_name' => array('LIKE' , '%'.$value['producer'].'%')))->getField('country_id');
            $id = $id ? $id : 45;
            $suplier_id = M('suplier')->where(array('code' => $value['organ']))->getField('erp_id');
            M('goods')->add(array(
                'name' => $value['name'],
                'stock_num' => $value['stock'],
                'earn13' => $value['code'],
                'price' => $value['price'],
                'goods_type_id' => $value['sort'],
                'extra_info' => "商品规格：{$value['spec']},件规格：{$value['conversion']}，计价方式：".($value['jjfs'] ? '计重（按重量销售）' : '计件（按商品数量销售)') . "，品牌编码：{$value['mark']}",
                'pom' => $value['unit'],
                'made_id' => $id,
                'supply_id' => $suplier_id,
                'die_time' => date('Y-m-d 00:00:00' , strtotime('+'.$value['keepday'].' day'))
            ));
            $this->model->save(array('id' => $value['id'] , 'is_sync' => 1));
            unset($data[$key]);
        }
    }

    //erp注册
    public function registerERP(){
        $data = M('goods')->where(array('_string' => 'ISNULL(sku) = 1'))->limit(1)->field('id')->select();
        foreach( $data as $k => $v ) {
            $this->erpApiObject->goods_reg( $v['id'] );
        }
    }

    //同步商品信息到盛汇

    public function syncGoods(){
    }

    public function test($id = 1) {
        set_time_limit(0);
        $this->__construct();
        $model = M('temp_xilian_data');
        $erpApiObject = new \Qwadmin\Model\ErpApiModel();
        //获得最新的产品列表
        /*


        
        //获得库存
        $data = $model->where(array('_string' => '`organ` != "" AND stock = 0'))->field('organ,code')->select();
        foreach( $data as $key => $value ) {
            $stock_info = \Qwadmin\Extend\Xilian::init()->query_stock_and_price( $value['code'] , $value['organ'] );
            $model->where(array('code' => $value['code']))->save(array('stock' => $stock_info['data']['Stock']));
        }*/
        


        


    }
    
}
